<template>
  <el-dialog
    :title="!dataForm.smsKanshopId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="pmsProductId">
      <el-input v-model="dataForm.pmsProductId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="pmsProductName">
      <el-input v-model="dataForm.pmsProductName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="pmsProductPrice">
      <el-input v-model="dataForm.pmsProductPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="商品低价" prop="smsKanshopMinPrice">
      <el-input v-model="dataForm.smsKanshopMinPrice" placeholder="商品低价"></el-input>
    </el-form-item>
    <el-form-item label="应砍刀数" prop="smsKanshopDaoNum">
      <el-input v-model="dataForm.smsKanshopDaoNum" placeholder="应砍刀数"></el-input>
    </el-form-item>
    <el-form-item label="单刀MAX钱" prop="smsKanshopDaoMaxPrice">
      <el-input v-model="dataForm.smsKanshopDaoMaxPrice" placeholder="单刀MAX钱"></el-input>
    </el-form-item>
    <el-form-item label="活动开始时间" prop="smsKanshopStartDate">
      <el-input v-model="dataForm.smsKanshopStartDate" placeholder="活动开始时间"></el-input>
    </el-form-item>
    <el-form-item label="活动结束时间" prop="smsKanshopEndDate">
      <el-input v-model="dataForm.smsKanshopEndDate" placeholder="活动结束时间"></el-input>
    </el-form-item>
    <el-form-item label="活动描述" prop="smsKanshopMiaoshu">
      <el-input v-model="dataForm.smsKanshopMiaoshu" placeholder="活动描述"></el-input>
    </el-form-item>
    <el-form-item label="砍价有效时间/分" prop="smsKanshopKanDate">
      <el-input v-model="dataForm.smsKanshopKanDate" placeholder="砍价有效时间/分"></el-input>
    </el-form-item>
    <el-form-item label="状态（1.活动中2.结束）" prop="smsKanshopStatus">
      <el-input v-model="dataForm.smsKanshopStatus" placeholder="状态（1.活动中2.结束）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          smsKanshopId: 0,
          pmsProductId: '',
          pmsProductName: '',
          pmsProductPrice: '',
          smsKanshopMinPrice: '',
          smsKanshopDaoNum: '',
          smsKanshopDaoMaxPrice: '',
          smsKanshopStartDate: '',
          smsKanshopEndDate: '',
          smsKanshopMiaoshu: '',
          smsKanshopKanDate: '',
          smsKanshopStatus: ''
        },
        dataRule: {
          pmsProductId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          pmsProductName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          pmsProductPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          smsKanshopMinPrice: [
            { required: true, message: '商品低价不能为空', trigger: 'blur' }
          ],
          smsKanshopDaoNum: [
            { required: true, message: '应砍刀数不能为空', trigger: 'blur' }
          ],
          smsKanshopDaoMaxPrice: [
            { required: true, message: '单刀MAX钱不能为空', trigger: 'blur' }
          ],
          smsKanshopStartDate: [
            { required: true, message: '活动开始时间不能为空', trigger: 'blur' }
          ],
          smsKanshopEndDate: [
            { required: true, message: '活动结束时间不能为空', trigger: 'blur' }
          ],
          smsKanshopMiaoshu: [
            { required: true, message: '活动描述不能为空', trigger: 'blur' }
          ],
          smsKanshopKanDate: [
            { required: true, message: '砍价有效时间/分不能为空', trigger: 'blur' }
          ],
          smsKanshopStatus: [
            { required: true, message: '状态（1.活动中2.结束）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.smsKanshopId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.smsKanshopId) {
            this.$http({
              url: this.$http.adornUrl(`/tgou/kanshop/info/${this.dataForm.smsKanshopId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pmsProductId = data.kanshop.pmsProductId
                this.dataForm.pmsProductName = data.kanshop.pmsProductName
                this.dataForm.pmsProductPrice = data.kanshop.pmsProductPrice
                this.dataForm.smsKanshopMinPrice = data.kanshop.smsKanshopMinPrice
                this.dataForm.smsKanshopDaoNum = data.kanshop.smsKanshopDaoNum
                this.dataForm.smsKanshopDaoMaxPrice = data.kanshop.smsKanshopDaoMaxPrice
                this.dataForm.smsKanshopStartDate = data.kanshop.smsKanshopStartDate
                this.dataForm.smsKanshopEndDate = data.kanshop.smsKanshopEndDate
                this.dataForm.smsKanshopMiaoshu = data.kanshop.smsKanshopMiaoshu
                this.dataForm.smsKanshopKanDate = data.kanshop.smsKanshopKanDate
                this.dataForm.smsKanshopStatus = data.kanshop.smsKanshopStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tgou/kanshop/${!this.dataForm.smsKanshopId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'smsKanshopId': this.dataForm.smsKanshopId || undefined,
                'pmsProductId': this.dataForm.pmsProductId,
                'pmsProductName': this.dataForm.pmsProductName,
                'pmsProductPrice': this.dataForm.pmsProductPrice,
                'smsKanshopMinPrice': this.dataForm.smsKanshopMinPrice,
                'smsKanshopDaoNum': this.dataForm.smsKanshopDaoNum,
                'smsKanshopDaoMaxPrice': this.dataForm.smsKanshopDaoMaxPrice,
                'smsKanshopStartDate': this.dataForm.smsKanshopStartDate,
                'smsKanshopEndDate': this.dataForm.smsKanshopEndDate,
                'smsKanshopMiaoshu': this.dataForm.smsKanshopMiaoshu,
                'smsKanshopKanDate': this.dataForm.smsKanshopKanDate,
                'smsKanshopStatus': this.dataForm.smsKanshopStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
